package org.abettor.pushbox.upload;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.model.SelfMessage;
import org.abettor.pushbox.net.NetTools;
import org.abettor.pushbox.util.FileUtil;
import org.abettor.pushbox.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * 上传地图的操作
 */
public class UploadMap {
	// 更新数据库的操作
	public static interface UpdateUploadMap {
		public void update(int id);
	}

	// 更新昵称的操作
	public static interface UpdateNick {
		public void update();
	}

	private Activity context;
	private SelfMessage self;
	private EditText nickName;
	private TextView showTextView;
	private Handler mHandler = new Handler();
	private String nickStr;// 昵称的名字
	private String password1Str;// 输入的密码
	private String password2Str;// 确认的密码
	private String passwordStr;// 旧密码
	private EditText password1;
	private EditText password;
	private EditText password2;
	private EditText psEditText;// 附言编辑的操作
	private String psStr;// 附言的内容
	private Spinner mapuloadtypeSpinner;
	private String[] mapuploadtypearray = null;
	private int mapuploadtype;// 地图上传时选择的种类
	private NetTools netTools = new NetTools();
	private static final int LOGIN_MAX_COUNT = 3;
	private static final int UPDATE_MAX_COUNT = 3;
	private int login_count = 0;// 记录的次数
	private int updatepass_count = 0;// 更新密码的操作
	private Runnable uploadFileRun = null;// 上传文件的run的操作
	private UpdateUploadMap updateMap = null;// 更新地图数据库的操作

	// private UpdateNick updateNick = null;//更新昵称的操作

	public UploadMap(Activity context) {
		this.context = context;
		mapuploadtypearray = new String[] {
				context.getText(R.string.upload_map_type_netusermap).toString(),
				context.getText(R.string.upload_map_type_mapquestion)
						.toString() };

	}

	/**
	 * 意见反馈的操作
	 * @param content
	 * @param email
	 */
	public void replyMsg(String content,String email){
		//内容不能为空
		if(content == null || content.equals("")){
			showRlyContentCantNullDialog();
			return;
		}
		if(email != null && ! email.equals(""))
		{
			String regex = "^([a-zA-Z0-9]+[_|\\-|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\-|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,4}$";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(email);
			if(! matcher.matches())
			{
				showRlyEmailErrorDialog();
				return;
			}
		}
		//显示上传的对话框
		sendRplyMsg(content, email);
	}

	/**
	 * 增加昵称的操作
	 */
	public void addNickName(final UpdateNick nick) {
		uploadFileRun = new Runnable() {
			@Override
			public void run() {
				// 显示更新成功的对话框
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						showUploadNickSuccDialog(nick);
					}
				});
			}
		};
		showNickDialog();
	}

	/**
	 * 更新密码的操作
	 */
	public void updatePass() {
		updatepass_count = 0;// 计数器归0
		// 显示录入密码的对话框
		showChangePass(R.string.change_pass);
	}

	/**
	 * 标记网络地图为解决的的
	 */
	public void markNetMapResolved(final int mapId) {
		self = Config.readSelfMessage(context);
		uploadFileRun = new Runnable() {
			@Override
			public void run() {
				updateNetMapState(mapId);// 把网络地图标记为解决的操作
			}
		};
		// 显示昵称的操作
		if (self == null) {
			showNickDialog();
		} else {
			nickStr = self.getNickName();
			Thread thread = new Thread(uploadFileRun);
			thread.start();
			// mHandler.post(uploadFileRun);
		}
	}

	/**
	 * 上传求解地图的解法的操作
	 * 
	 * @param buf
	 */
	public void uploadQuesstionMapResolved(final String file, final int mapId) {
		self = Config.readSelfMessage(context);
		uploadFileRun = new Runnable() {
			@Override
			public void run() {
				uploadQuesstionMapResolvedOption(file, mapId);
			}
		};
		nickStr = self.getNickName();
		Thread thread = new Thread(uploadFileRun);
		thread.start();
	}

	/**
	 * 预上传地图的操作
	 */
	public void uploadMap(final byte[] buf, UpdateUploadMap update) {
		updateMap = update;
		// 先查找是否已经设置了昵称
		self = Config.readSelfMessage(context);
		// 上传文件后需要运行的Runnable
		uploadFileRun = new Runnable() {
			@Override
			public void run() {
				uploadMapFile(buf);
			}
		};
		if (self == null) {
			// 显示设置昵称的操作
			showNickDialog();
		} else {
			nickStr = self.getNickName();
			Thread thread = new Thread(uploadFileRun);
			thread.start();
		}
	}

	// 上传文件的操作,先设置要上传的地图的类型，再进行操作
	private void uploadMapFile(final byte[] buf) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				// psStr = null;// 把附言设为null
				mapuploadtype = 0;// 初始化地图的种类
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				AlertDialog alert = builder.create();
				alert.setTitle(R.string.upload_map);
				LayoutInflater inflater = context.getLayoutInflater();
				final ViewGroup view = (ViewGroup) inflater.inflate(
						R.layout.upload_map_settype_dialog, null, false);
				mapuloadtypeSpinner = (Spinner) view
						.findViewById(R.id.mapuloadtypeSpinner);
				psEditText = (EditText) view.findViewById(R.id.upload_map_ps);
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(
						context, android.R.layout.simple_spinner_item,
						mapuploadtypearray);
				// 设置下拉列表的风格
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				mapuloadtypeSpinner.setAdapter(adapter);
				// 设置监听器
				mapuloadtypeSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {
							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View view, int arg2, long arg3) {
								if (mapuploadtypearray[arg2]
										.equals(context
												.getText(
														R.string.upload_map_type_netusermap)
												.toString())) {
									mapuploadtype = ParaUploadRetValue.PUSH_BOX_UPLOAD_TYPE_NETUSERMAP;
								} else if (mapuploadtypearray[arg2]
										.equals(context
												.getText(
														R.string.upload_map_type_mapquestion)
												.toString())) {
									mapuploadtype = ParaUploadRetValue.PUSH_BOX_UPLOAD_TYPE_QUESSIONMAP;
								}
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								mapuploadtype = ParaUploadRetValue.PUSH_BOX_UPLOAD_TYPE_NETUSERMAP;
							}
						});
				mapuloadtypeSpinner.setVisibility(View.VISIBLE);
				alert.setView(view);
				// 设置按钮
				alert.setButton(DialogInterface.BUTTON_NEGATIVE,
						context.getText(android.R.string.cancel),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								 Editable editable =
								 psEditText.getEditableText();
								 psStr = editable.toString();
								 if(psStr != null)
								 {
									 psStr = psStr.trim();
								 }
								uploadingMapFile(buf);// 实际上传文件的操作
							}
						});
				alert.show();
			}
		});
	}

	// 上传求解地图答案的操作
	private void uploadQuesstionMapResolvedOption(final String fileName,
			final int mapId) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				progress = new ProgressDialog(context);
				progress.setTitle(R.string.quesstion_map_uploadresolved);
				progress.setMessage(context
						.getText(R.string.quesstion_map_uploadresolved_ing));
				progress.show();
				// 更新网络状态
				Thread thread = new Thread() {
					@Override
					public void run() {
						byte[] buf = null;
						try {
							buf = FileUtil.readFileByPath(fileName);
						} catch (IOException e) {
						}
						Map<String, String> hashMap = new HashMap<String, String>();
						hashMap.put("nickName", nickStr);
						hashMap.put("mapId", String.valueOf(mapId));
						String ret = null;
						if (buf != null) {
							ret = netTools.postFile(Config.uploadAnswer,
									"answer", "application/octet-stream", buf,
									hashMap);
						}
						progress.dismiss();
						if (ret == null) {
							readMessageError();// 读取网络消息失败
						} else {// 解析返回的字符串
							boolean flag = ParaUploadRetValue.isAnswerUploadSuccess(ret);
							if (flag) {// 上传成功
								mHandler.post(new Runnable() {
									@Override
									public void run() {
										showAnswerUploadSuccessDialog();// 显示上传地图成功的操作
									}
								});
							} else {
								mHandler.post(new Runnable() {
									@Override
									public void run() {
										showAnswerdFailDialog();// 显示上传地图失败的对话框
									}
								});
							}
						}
					}
				};
				thread.start();
			}
		});
	}
	// 提交反馈意见的操作
	private void sendRplyMsg(final String content, final String mail) {
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.reply_msg);
		progress.setMessage(context.getText(R.string.reply_msg_repling));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				Map<String, String> hashMap = new HashMap<String, String>();
				SelfMessage self = Config.readSelfMessage(context);
				if (self != null) {
					hashMap.put("nickName", self.getNickName());
				}
				hashMap.put("content", content);
				if(mail != null && !mail.equals("")){
					hashMap.put("email", mail);
				}
				String ret = netTools.postData(Config.reply, hashMap);
				progress.dismiss();
				if (ret == null) {
					sendRlpMsgErrorDialog();// 提交反馈消息失败的对话框
				}else{//解析返回的字符串
					boolean flg = ParaUploadRetValue.isSendRplyMsgSuccess(ret);
					if(flg){
						showSendRplySuccessDialog();
					}else{
						sendRlpMsgErrorDialog();// 提交反馈消息失败的对话框
					}
				}
			}
		};
		thread.start();
	}

	// 更新网络地图为解决的状态
	private void updateNetMapState(final int mapId) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				progress = new ProgressDialog(context);
				progress.setTitle(R.string.net_map_updatestate);
				progress.setMessage(context.getText(R.string.net_map_updating));
				progress.show();
				// 更新网络状态
				Thread thread = new Thread() {
					@Override
					public void run() {
						// 读取配置文件
						Map<String, String> hashMap = new HashMap<String, String>();
						hashMap.put("nickName", nickStr);
						hashMap.put("mapId", String.valueOf(mapId));
						String ret = netTools.postData(Config.markIResolved,
								hashMap);
						if (ret == null) {
							updateNetMapError(mapId);// 读取网络消息失败
						} else {// 解析返回的字符串
							boolean flag = ParaUploadRetValue
									.isUpadateNetStateSuccess(ret);
							if (flag) {
								showNetMapChangeSuccessDialog();
							} else {// 更新图片状态操作
								updateNetMapError(mapId);
							}
						}
					}
				};
				thread.start();
			}
		});
	}

	// 开始上传地图的操作
	private void uploadingMapFile(final byte[] buf) {
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.upload_result);
		progress.setMessage(context.getText(R.string.upload_maping));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				// 读取配置文件
				Map<String, String> hashMap = new HashMap<String, String>();
				hashMap.put("nickName", nickStr);
				hashMap.put("needAnswer", String.valueOf(mapuploadtype));
				//hashMap.put("summary", ps);
				if(psStr != null && !psStr.equals("")){
					hashMap.put("summary", psStr);
				}
				String ret = netTools.postFile(Config.uploadFileUrl, "map",
						"application/octet-stream", buf, hashMap);
				progress.dismiss();
				if (ret == null) {
					readMessageError();// 读取网络消息失败
				} else {// 解析返回的字符串
					final ParaUploadRetValue.UploadMapValue retVal = ParaUploadRetValue
							.paraRetValue(ret);
					if (retVal.getKey() == ParaUploadRetValue.UPLOAD_MAP_RET_SUCCESS) {// 上传成功
						// 更新数据库的操作
						if (updateMap != null) {
							updateMap.update(retVal.getId());
						}
						mHandler.post(new Runnable() {
							@Override
							public void run() {
								showUploadMapSuccessDialog();// 显示上传地图成功的操作
							}
						});
					} else {// 显示上传失败的对话狂
						mHandler.post(new Runnable() {
							@Override
							public void run() {
								showUploadMapFailDialog(retVal);
							}
						});
					}
				}
			}
		};
		thread.start();
	}

	// 写入配置文件
	private void writeSelfMessage(SelfMessage msg) {
		Config.writeSelfMessage(context, msg);
	}

	// 显示设置昵称的操作
	private void showNickDialog() {
		login_count = 0;// 归零次数
		alertMsg(R.string.input_nick);
	}

	// 设置昵称不能为空
	private void alertMsg(final int msgid) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.input_nick);
		Activity activity = (Activity) context;
		LayoutInflater inflater = activity.getLayoutInflater();
		final ViewGroup view = (ViewGroup) inflater.inflate(
				R.layout.input_nick_dialog, null, false);
		nickName = (EditText) view.findViewById(R.id.nickname);
		showTextView = (TextView) view.findViewById(R.id.show_msg);
		showTextView.setText(msgid);
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Editable editable = nickName.getEditableText();
						if (editable.toString().equals("")) {// 昵称不能为空
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									alertMsg(R.string.input_nick_notnull);
								}
							});
						} else {// 昵称不为空时检查昵称是否已经存在
							checkNickName();
						}
					}
				});
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,
				context.getText(android.R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.setView(view);
		alert.show();
	}

	private ProgressDialog progress;// 进度条对话框

	// 进行登录的操作
	private void loginNickName(final Runnable run) {
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.login_nick);
		progress.setMessage(context.getText(R.string.login_nicking));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				Map<String, String> map = new HashMap<String, String>();
				map.put("nickName", nickStr);
				map.put("passWord", password1Str);
				// map.put("ps", psStr);
				String ret = netTools.postData(Config.loginNickUrl, map);
				if (ret == null) {
					progress.dismiss();
					readMessageError();
					return;
				} else {
					progress.dismiss();
					boolean flag = ParaUploadRetValue.isLoginNickSuccess(ret);
					if (flag) {
						// 写入配置文件
						SelfMessage s = new SelfMessage();
						s.setNickName(nickStr);
						writeSelfMessage(s);
						if (run != null)
							run.run();
						return;
					} else {
						login_count++;// 进行计数的操作
						if (login_count < LOGIN_MAX_COUNT) {// 当每超过三次时，提示重新输入
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									loginPassword(R.string.input_pass_notright);
								}
							});
						} else {
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									showPasswordNotRightDialog();
								}
							});
						}
						return;
					}
				}
			}
		};
		thread.start();
	}

	// 更新昵称
	private void uploadNickName(final Runnable run) {
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.add_nick);
		Activity activity = (Activity) context;
		progress.setMessage(activity.getText(R.string.input_nick_checking));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				Map<String, String> map = new HashMap<String, String>();
				map.put("nickName", nickStr);
				map.put("passWord", password1Str);
				String ret = netTools.postData(Config.uploadNickUrl, map);
				if (ret == null) {// 读取数据失败
					progress.dismiss();
					readMessageError();
					return;
				} else {
					progress.dismiss();
					int flag = 0;
					try {
						flag = ParaUploadRetValue.isUploadNickSuccess(ret);
						if (flag == ParaUploadRetValue.UPLOAD_NICK_SUCCESS) {
							// 更新昵称成功，写记录
							SelfMessage s = new SelfMessage();
							s.setNickName(nickStr);
							writeSelfMessage(s);
							// to-do 数据上传的操作
							if (run != null)
								run.run();
						} else if (flag == ParaUploadRetValue.UPLOAD_NICK_EXITS) {// 昵称已经存在
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									showUploadNickExistsDialog();
								}
							});
						} else {
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									showUploadNickFailDialog();
								}
							});
						}
					} catch (Exception e) {
						readMessageError();
						return;
					}
				}
			}
		};
		thread.start();
	}

	// 显示更新成功的对话框
	private void showUploadNickSuccDialog(final UpdateNick nick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.add_nick);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.add_nick_success));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						nick.update();
					}
				});
		alert.show();
	}

	// 显示反馈内容不能为空的对话框
	private void showRlyContentCantNullDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.reply_msg);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.reply_msg_cant_null));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示Email格式不对的对话框
	private void showRlyEmailErrorDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.reply_msg);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.reply_msg_email_error));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示更新昵称失败的对话框
	private void showUploadNickFailDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.add_nick);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.add_nick_fail));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示更新地图正确的对话框
	private void showNetMapChangeSuccessDialog() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				AlertDialog alert = builder.create();
				alert.setTitle(R.string.success);
				Activity activity = (Activity) context;
				alert.setMessage(activity
						.getText(R.string.net_map_update_success));
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								context.onBackPressed();
							}
						});
				alert.show();
			}
		});
	}

	// 上传答案成功的对话框
	private void showAnswerUploadSuccessDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.success);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.answer_upload_success));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						context.onBackPressed();
					}
				});
		alert.show();
	}
	//显示发送消息正确的对话框
	private void showSendRplySuccessDialog(){
		mHandler.post(new Runnable(){
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				AlertDialog alert = builder.create();
				alert.setTitle(R.string.success);
				Activity activity = (Activity) context;
				alert.setMessage(activity.getText(R.string.reply_msg_success));
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								context.onBackPressed();
							}
						});
				alert.show();
			}
		});
	}
	// 显示上传地图正确的对话框
	private void showUploadMapSuccessDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.success);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.upload_map_success));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示修改密码成功的对话框
	private void showUpdatePassSuccessDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.success);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.change_pass_success));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示更改密码失败的对话框
	private void showUpdatePassFailDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.success);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.change_pass_fail));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示上传答案错误的对话框
	private void showAnswerdFailDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.error);
		alert.setMessage(context.getText(R.string.answer_upload_fail));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示上传地图错误的对话框
	private void showUploadMapFailDialog(ParaUploadRetValue.UploadMapValue value) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.error);
		if (value.getKey() == ParaUploadRetValue.UPLOAD_MAP_RET_MAPHASEXIST) {
			alert.setMessage(context.getText(R.string.upload_map_mapexist));
		} else if (value.getKey() == ParaUploadRetValue.UPLOAD_MAP_RET_OTHERERROR) {
			alert.setMessage(context.getText(R.string.upload_map_othererror));
		}
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示密码不正确的的对话框
	private void showPasswordNotRightDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.error);
		alert.setMessage(context.getText(R.string.loginin_passnotright));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						alertMsg(R.string.input_nick);
					}
				});
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,
				context.getText(android.R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 显示更新昵称昵称已经存在的对话框
	private void showUploadNickExistsDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.add_nick);
		Activity activity = (Activity) context;
		alert.setMessage(activity.getText(R.string.add_nick_exist));
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						alertMsg(R.string.input_nick);
					}
				});
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,
				context.getText(android.R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.show();
	}

	// 检查昵称是否存在
	private void checkNickName() {
		Editable editable = nickName.getEditableText();
		nickStr = editable.toString().trim();
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.input_nick_check);
		Activity activity = (Activity) context;
		progress.setMessage(activity.getText(R.string.input_nick_checking));
		progress.show();
		// 检查地图是否存在的线程
		Thread thread = new Thread() {
			@Override
			public void run() {
				Map<String, String> map = new HashMap<String, String>();
				map.put("nickName", nickStr);
				String ret = netTools.postData(Config.checkNickUrl, map);
				if (ret == null) {// 读取数据失败
					progress.dismiss();
					readMessageError();
					return;
				} else {
					progress.dismiss();
					boolean flag = false;
					try {
						flag = ParaUploadRetValue.isNickNameExits(ret);
						if (flag) {// 昵称已经存在,显示登录密码的对话框
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									loginPassword(R.string.input_pass);
								}
							});
							return;
						} else {//
							// 显示输入密码的对话框
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									inputPassword(R.string.input_pass);
								}
							});
						}
					} catch (Exception e) {
						readMessageError();
						return;
					}
				}
			}
		};
		thread.start();
	}

	// 显示更新密码的操作
	private void showChangePass(final int msgid) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.change_pass);
		LayoutInflater inflater = context.getLayoutInflater();
		final ViewGroup view = (ViewGroup) inflater.inflate(
				R.layout.change_pass_dialog, null, false);
		TextView showMsg = (TextView) view.findViewById(R.id.show_msg);
		showMsg.setText(msgid);
		password1 = (EditText) view.findViewById(R.id.new_password1);
		password2 = (EditText) view.findViewById(R.id.new_password2);
		password = (EditText) view.findViewById(R.id.oldpassword);
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,
				context.getText(android.R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Editable editable = password1.getEditableText();
						password1Str = editable.toString();
						editable = password.getEditableText();
						passwordStr = editable.toString();
						editable = password2.getEditableText();
						password2Str = editable.toString();
						if (passwordStr.equals("") || password1Str.equals("")
								|| password2Str.equals("")) {// 密码不能为空
							showChangePass(R.string.input_pass_notnull);
							return;
						}
						if (!password1Str.equals(password2Str)) {// 两次密码不相等
							showChangePass(R.string.input_pass_notequal);
							return;
						}
						// 更新密码的操作
						updatePassWord(passwordStr, password1Str);
					}
				});
		alert.setView(view);
		alert.show();
	}

	// 更新密码的操作
	private void updatePassWord(String pass1, String pass2) {
		progress = new ProgressDialog(context);
		progress.setTitle(R.string.change_pass);
		progress.setMessage(context.getText(R.string.change_passing));
		progress.show();
		Thread thread = new Thread() {
			@Override
			public void run() {
				self = Config.readSelfMessage(context);
				nickStr = self.getNickName();
				Map<String, String> map = new HashMap<String, String>();
				map.put("nickName", nickStr);
				map.put("passWord", passwordStr);
				map.put("newPassword", password1Str);
				String ret = netTools.postData(Config.modifyPassword, map);
				if (ret == null) {
					progress.dismiss();
					readMessageError();
					return;
				} else {
					progress.dismiss();
					boolean flag = ParaUploadRetValue.isUpdatePassSuccess(ret);
					if (flag) {// 显示修改密码成功的对话框
						mHandler.post(new Runnable() {
							@Override
							public void run() {
								showUpdatePassSuccessDialog();
							}
						});
						return;
					} else {
						updatepass_count++;// 进行计数的操作
						if (updatepass_count < UPDATE_MAX_COUNT) {// 当每超过三次时，提示重新输入
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									showChangePass(R.string.change_pass_fail_retry);
								}
							});
						} else {
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									showUpdatePassFailDialog();
								}
							});
						}
						return;
					}
				}
			}
		};
		thread.start();
	}

	// 用户输入确认的的密码
	private void loginPassword(final int msgid) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.input_pass);
		LayoutInflater inflater = context.getLayoutInflater();
		final ViewGroup view = (ViewGroup) inflater.inflate(
				R.layout.login_pass_dialog, null, false);
		TextView textView = (TextView) view.findViewById(R.id.show_msg);
		textView.setText(msgid);
		password1 = (EditText) view.findViewById(R.id.password);
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,
				context.getText(android.R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Editable editable = password1.getEditableText();
						password1Str = editable.toString();
						if (password1Str.equals("")) {// 密码不能为空
							loginPassword(R.string.input_pass_notnull);
						} else {// 进行登录的操作
							loginNickName(uploadFileRun);// 上传地图的操作
						}
					}
				});
		alert.setView(view);
		alert.show();
	}

	// 输入密码的对话框
	private void inputPassword(final int msgid) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.input_pass);
		Activity activity = (Activity) context;
		LayoutInflater inflater = activity.getLayoutInflater();
		final ViewGroup view = (ViewGroup) inflater.inflate(
				R.layout.create_pass_dialog, null, false);
		TextView textView = (TextView) view.findViewById(R.id.show_msg);
		textView.setText(msgid);
		password1 = (EditText) view.findViewById(R.id.password);
		password2 = (EditText) view.findViewById(R.id.password2);
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,
				context.getText(android.R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		alert.setButton(DialogInterface.BUTTON_POSITIVE,
				context.getText(android.R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Editable editable = password1.getEditableText();
						password1Str = editable.toString();
						editable = password2.getEditableText();
						password2Str = editable.toString();
						if (!password1Str.equals(password2Str)) {// 密码不相等
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									inputPassword(R.string.input_pass_notequal);
								}
							});
							return;
						} else if (password1Str.equals("")) {
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									inputPassword(R.string.input_pass_notnull);
								}
							});
							return;
						} else {// 上传用户名和密码的操作
							uploadNickName(uploadFileRun);// 进行逻辑的操祖
						}
					}
				});
		alert.setView(view);
		alert.show();
	}

	// 读取网络信息失败,显示的对话框
	private void readMessageError() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				final AlertDialog alert = builder.create();
				alert.setTitle(R.string.error);
				Activity activity = (Activity) context;
				alert.setMessage(activity.getText(R.string.read_net_error));
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				alert.show();
			}
		});
	}

	// 提交反馈消息错误的对话框
	private void sendRlpMsgErrorDialog() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				final AlertDialog alert = builder.create();
				alert.setTitle(R.string.error);
				Activity activity = (Activity) context;
				alert.setMessage(activity.getText(R.string.read_net_error));
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				alert.show();
			}
		});
	}

	// 更新网络地图状态失败,显示的对话框
	private void updateNetMapError(final int netId) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				final AlertDialog alert = builder.create();
				alert.setTitle(R.string.error);
				Activity activity = (Activity) context;
				alert.setMessage(activity.getText(R.string.read_net_error));
				alert.setButton(DialogInterface.BUTTON_POSITIVE,
						context.getText(R.string.retry),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								markNetMapResolved(netId);
							}
						});
				alert.setButton(DialogInterface.BUTTON_NEGATIVE,
						context.getText(android.R.string.cancel),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				alert.show();
			}
		});
	}
}
